﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MoodleDesktop
{
    public partial class fmCalendar : Form
    {
        public static SelectingSource source;
        public static fmForum forum;
        public static fmSource source;
        public static List<Day> dsDay = new List<Day>();
        public static Point vitri;

        private NotifyIcon  trayIcon;
        private ContextMenu trayMenu;

        private bool mousePressed = false;
        private Point diff = new Point(0, 0);

        



        public fmCalendar()
        {
            InitializeComponent();
            this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Size.Width, 50);
            this.AddEventHandler(this);
        }
        private void AddEventHandler(Control ctl)
        {
            ctl.MouseDown += new MouseEventHandler(ctl_MouseDown);
            ctl.MouseMove += new MouseEventHandler(ctl_MouseMove);
            ctl.MouseUp += new MouseEventHandler(ctl_MouseUp);
        }

        void ctl_MouseUp(object sender, MouseEventArgs e)
        {
            mousePressed = false;
        }

        void ctl_MouseMove(object sender, MouseEventArgs e)
        {
            if (mousePressed && (e.Button & MouseButtons.Left) != 0)
            {
                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                p.X -= diff.X;
                p.Y -= diff.Y;
                DesktopLocation = p;
                vitri = p;
            }    
        }

        void ctl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mousePressed = true;

                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                diff.X = p.X - DesktopLocation.X;
                diff.Y = p.Y - DesktopLocation.Y;
            }
        }
        private void Calendar_Load(object sender, EventArgs e)
        {
            //this.ShowInTaskbar = false;
            //this.ParentForm.Close();
            vitri = this.Location;
            DateTime date = new DateTime(2012, 4, 29);

            // hide in task bar
            //this.ShowInTaskbar = false;


            for (int i = 0; i < 35;i++ )
            {
                
                Day d = new Day();
                d.date = date;
                d.label_Day.Text = date.Day.ToString();
                if (date.Month != 5)
                    d.BackgroundImage = Resource1.Day1;
                else
                    d.BackgroundImage = null;
                
                if (date.Day == DateTime.Now.Day)
                    d.BackgroundImage = Resource1.nowDay;
                if (date.Day == 17)
                {
                    d.BackgroundImage = Resource1.activeday;
                    d.isActiveDay = true;
                }
                panel_DayofMonth.Controls.Add(d);
                if (date.Day == DateTime.DaysInMonth(date.Year,date.Month))
                    date = new DateTime(date.Year, date.Month + 1, 1);
                else
                    date = new DateTime(date.Year, date.Month,date.Day + 1);

                //d.Click += new EventHandler(d_Click);
                //d.mySender = new Day.clicktoDay(d_Click);
                dsDay.Add(d);
            }
            
            // Add tray icon
            trayIcon = new NotifyIcon();
            trayIcon.Text = "Moodle Desktop";
            trayIcon.Visible = true;
            trayIcon.Icon = new Icon("trayicon.ico");

            trayMenu = new ContextMenu();
            
            trayMenu.MenuItems.Add("Show in Taskbar", new System.EventHandler(ShowInTaskbar_Click));
            trayMenu.MenuItems.Add("Hide in Taskbar", new System.EventHandler(HideInTaskbar_Click));
            trayMenu.MenuItems.Add("Exit", new System.EventHandler(Exit_Click));
            //trayMenu.MenuItems.Add(0, new MenuItem("Show", new System.EventHandler(Exit_Click)));
            trayIcon.ContextMenu = trayMenu;
        }

        protected void Exit_Click(Object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        protected void ShowInTaskbar_Click(Object sender, System.EventArgs e)
        {
            this.ShowInTaskbar = true;
            
        }
        protected void HideInTaskbar_Click(Object sender, System.EventArgs e)
        {
            this.ShowInTaskbar = false;

        }

        private void btn_deadline_Click(object sender, EventArgs e)
        {
        }

        private void btn_deadline_MouseEnter(object sender, EventArgs e)
        {
            btn_deadline.Image = Resource1.deadline_hover;
        }

        private void btn_deadline_MouseHover(object sender, EventArgs e)
        {
            btn_deadline.Image = Resource1.deadline_hover;
        }

        private void btn_deadline_MouseLeave(object sender, EventArgs e)
        {
            btn_deadline.Image = Resource1.deadline;
        }

        private void btn_thongbao_MouseEnter(object sender, EventArgs e)
        {
            btn_thongbao.Image = Resource1.thongbao_hover;
        }

        private void btn_thongbao_MouseHover(object sender, EventArgs e)
        {
            btn_thongbao.Image = Resource1.thongbao_hover;
        }

        private void btn_thongbao_MouseLeave(object sender, EventArgs e)
        {
            btn_thongbao.Image = Resource1.thongbao;
        }

        private void btn_forum_MouseEnter(object sender, EventArgs e)
        {
            btn_forum.Image = Resource1.forum_hover;
        }

        private void btn_forum_MouseHover(object sender, EventArgs e)
        {
            btn_forum.Image = Resource1.forum_hover;
        }

        private void btn_forum_MouseLeave(object sender, EventArgs e)
        {
            btn_forum.Image = Resource1.forum;
        }

        private void btn_tailieu_MouseEnter(object sender, EventArgs e)
        {
            btn_tailieu.Image = Resource1.document_hover;
        }

        private void btn_tailieu_MouseHover(object sender, EventArgs e)
        {
            btn_tailieu.Image = Resource1.document_hover;
        }

        private void btn_tailieu_MouseLeave(object sender, EventArgs e)
        {
            btn_tailieu.Image = Resource1.document;
        }

        private void btn_forum_Click(object sender, EventArgs e)
        {
            //if(forum != null)
            //    forum.Close();
            //forum = new fmForum();
            //forum.Location = new Point(this.Location.X - 300, this.Location.Y);
            //forum.Show();
            if (source != null)
                source.Close();
            source = new fmSource();
            source.Location = new Point(this.Location.X - 300, this.Location.Y);
            source.Show();
        }

        private void fmCalendar_FormClosed(object sender, FormClosedEventArgs e)
        {
            trayIcon.Dispose();
            Application.Exit();
            //this.ShowInTaskbar = false;
            //this.WindowState = FormWindowState.Minimized;
        }
    }
}
